package com.zys.controller;

import com.zys.pojo.Role;
import com.zys.entity.Result;
import com.zys.service.RoleService;
import com.zys.constant.MessageConstant;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理角色相关的操作
 */
@RestController
@RequestMapping("/role")
public class RoleController {

  @Reference
  private RoleService roleService;

  /**
   * 获取角色列表
   *
   * @return Result
   */
  @RequestMapping("/findAll")
  public Result findAll() {
    try {
      return new Result(true, MessageConstant.GET_ROLE_LIST_SUCCESS, roleService.findAll());
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.GET_ROLE_LIST_FAIL);
    }
  }

  /**
   * 添加角色
   *
   * @param rightsId 分配给该角色的权限 id 数组
   * @param role     角色信息
   * @return Result
   */
  @RequestMapping("/add")
  public Result add(@RequestParam Integer[] rightsId, @RequestBody Role role) {
    try {
      roleService.add(rightsId, role);
      return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.ADD_ROLE_FAIL);
    }
  }

  /**
   * 根据 id 获取角色信息
   *
   * @param id 角色 id
   * @return Result
   */
  @RequestMapping("/getRoleById")
  public Result getRoleById(Integer id) {
    try {
      return new Result(true, MessageConstant.GET_ROLE_SUCCESS, roleService.findRoleById(id));
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.GET_ROLE_FAIL);
    }
  }

  /**
   * 修改角色基本信息
   *
   * @param role 角色基本信息
   * @return Result
   */
  @RequestMapping("/edit")
  public Result edit(@RequestBody Role role) {
    try {
      roleService.edit(role);
      return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
    }
  }

  /**
   * 根据角色 id 获取角色所拥有的权限 id
   *
   * @param id 角色 id
   * @return Result
   */
  @RequestMapping("/findRightsByRoleId")
  public Result findRightsByRoleId(Integer id) {
    try {
      return new Result(true, MessageConstant.GET_ROLE_RIGHTS_SUCCESS, roleService.findRightsIdsByRoleId(id));
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.GET_ROLE_RIGHTS_FAIL);
    }
  }

  /**
   * 为角色分配权限
   *
   * @param id       角色 id
   * @param rightsId 权限 id 数组
   * @return Result
   */
  @RequestMapping("/setDistribution")
  public Result setDistribution(@RequestParam Integer id, @RequestBody Integer[] rightsId) {
    try {
      roleService.setDistribution(id, rightsId);
      return new Result(true, MessageConstant.SET_ROLE_RIGHTS_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.SET_ROLE_RIGHTS_FAIL);
    }
  }

  /**
   * 根据 id 删除角色
   *
   * @param id 角色 id
   * @return Result
   */
  @RequestMapping("/delete")
  public Result delete(@RequestParam Integer id) {
    try {
      roleService.delete(id);
      return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
    }
  }
}
